<?php
/***
 * 描述：进货单管理[店长权限]
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: StockOrderManageAction.class.php 34 2013-10-27 02:29:45Z whtaok@gmail.com $
 */
class StockOrderManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="ProductPrice";
	}
	
	/**
	 * 进货管理一览首页面
	 * 根据shop_id查询进货信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$search = array(
				'shop_id'=> $this->curr_shop_id,
				'product_code'=> I('product_code'),
				'product_stock_status'=>I('product_stock_status',0,''),//默认查询未入库商品 0，未入库 
				'supplyer'=>I('supplyer'),
		);
		
		$modWhere = array(
				'shop_id'=>$search['shop_id'],
				'product_stock_status'=>$search['product_stock_status'],
				'supplyer'=>$search['supplyer'],
				'product_code'=> I('product_code'),
		);
		$mod=new StockOrderModel();
		$totalRows = $mod->setModWhere($modWhere)->count();
		list($list,$page) = $mod->setModWhere($modWhere)->listPage($totalRows);

		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);

		$this->display('index');
	}
	
	
	/**
	 * 商品查询页面
	 * 根据shop_id查询进货信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-09-26
	 */
	public function showProductListAct(){
		$cateCode = I('cate_code');
		$search = array(
				'cate_code'=> I('cate_code'),
				'name'=>I('name'),
				'supplyer'=>I('supplyer'),
				'code'=>I('code'),
		);
		$modWhere = array(
				'cate_code2'=>array('like',$search['cate_code'].'%'),
				'name'=>array('like','%'.$search['name'].'%'),
				'code'=>array('like','%'.$search['code'].'%'),
		);
		$mod=new ProductLibModel();
		$totalRows = $mod->setModWhere($modWhere)->count();
		list($list,$page) = $mod->setModWhere($modWhere)->listPage($totalRows);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->assign('cateTree',productcate_tree());
		$this->display('product_list');
	}
	
	/**
	 * 创建订货单信息页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showAddAct() {
		$product_code = I('product_id');
		if($product_code){
			$mod = new ProductLibModel();
			$productModWhere['id'] = $product_code;
			$product = $mod->setModWhere($productModWhere)->find();
			$mod = new ProductPriceModel();
			$productPrice = $mod->setModWhere(array('prod_id'=>$product_code))->find();
		}
		
		$shop =  session(SES_SHOP);
		$shop['user'] = session(SES_USER);
		$this->assign('shop',$shop);
		$this->assign('product',$product);
		$this->assign('productPrice',$productPrice);
		$this->display('add');
	}
	
	/**
	 * 添加操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAct() {
		$data = array(
			'product_id'=>I('product_id'),
			'product_name'=>I('product_name'),
			'product_num'=>I('product_num'),
			'product_amount'=>I('product_amount'),
			'product_content'=>I('product_content'),
			'product_order_date'=>I('product_order_date'),
			'product_send_date'=>I('product_send_date'),
			'supplyer_id'=>I('supplyer_id'),
			'supplyer_content'=>I('supplyer_content'),
			'shop_id'=>$this->curr_shop_id,
			'shop_content'=>I('shop_content'),
			'shop_user_id'=>$this->curr_user_id,
			'shop_user_content'=>I('shop_user_content'),
			'product_stock_status'=>0,
		);
		$mod=new StockOrderModel();
		if($mod->create($data)){
			$lastId = $mod->add();
			$this->success('添加成功，请更改商品库存',U('showUpd',array('id'=>$lastId)));
		}else{
			$this->error($mod->getError());
		}
	}
	
	/**
	 * 显示编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdAct() {
		$id = I('id');
		$mod=new StockOrderModel();
		$modWhere = array(
			'id'=>$id,
			'shop_id'=>$this->curr_shop_id,
		);
		$detail=$mod->setModWhere($modWhere)->find();
		
		$mod = new ShopLibModel();
		$detail['shop']=$mod->find($detail['shop_id']);
		
		$this->assign('detail',$detail);
		$this->display('upd');
	}
	
	/**
	 * 编辑操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct() {
		$id = I('id');
		$data = array(
			'product_num'=>I('product_num'),
			'product_amount'=>I('product_amount'),
			'product_content'=>I('product_content'),
			'product_order_date'=>I('product_order_date'),
			'product_send_date'=>I('product_send_date'),
			'supplyer_id'=>I('supplyer_id'),
			'supplyer_content'=>I('supplyer_content'),
			'shop_content'=>I('shop_content'),
			'shop_user_content'=>I('shop_user_content'),
		);
		$mod = new StockOrderModel();
		if($mod->create($data)){
			$modWhere = array(
				'id'=>$id,
				'shop_id'=>$this->curr_shop_id,
			);
			$flag = $mod->where($modWhere)->save();
			if(flag !== false){
				$this->success('添加成功，请更改商品库存',U('showUpd',array('id'=>$id)));
			}else{
				$this->error('更新失败，请重新尝试');
			}
			
		}else{
			$this->error($mod->getError());
		}
	}
	

	/**
	 * 入库状态修改操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doStockStatusAct(){
		$id = I('id');
		$data = array(
			'product_stock_status'=>I('product_stock_status'),
		);
		if($id){
			$mod = new StockOrderModel();
			$modWhere['id'] = $id;
			$modWhere['shop_id']=$this->curr_shop_id;
			$mod->where($modWhere)->save($data);
		}
		$this->success('修改成功', U('index'));
	}	
	
	/**
	 * 删除操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct(){
		$id_list = array_values(I('id'));
		if($id_list){
			$mod = new StockOrderModel();
			$data['id'] = array('in',$id_list);
			$data['shop_id']=$this->curr_shop_id;
			$mod->where($data)->delete();
		}
		$this->success('删除成功', U('index'));
	}
}

?>